﻿package rob.entity{
	import flash.display.Sprite;
	import rob.util.NumberUtil;
	import rob.core.Poly3d;
	import rob.core.Base3d;
	import rob.data.BSqr;
	
	public class Building extends Entity{
		private var _base:Base3d;
		private var _bsqr:BSqr;
		private var _visible:Boolean;
		
		public function Building(s:Sprite):void{
			// create mesh
			super(s);
			_visible = true;
			// create base3d (bounds + ordering)
			_base = new Base3d(mesh.aBaseVertex,mesh.aBasePolygon);
			// also create bounding square (for ordering)
			// to check if hit *any* part of building (not just base)
			_bsqr = new BSqr();
		}

		// draw exact base of building (on ground)
		public function drawBase():void{
			_base.draw();
		}
		
		// compute bounding square and enlarge a bit to cover whole building
		public function computeBSqr():void{
			_bsqr.compute(mesh.aBaseVertex);
		}
		
		// set all polygons of buildings visible = true/false
		// only switch if b = !_visible
		public function setVisible(b:Boolean):void{
			var p:Poly3d;
			if(!b && _visible){
				for each(p in mesh.aPolygon) p.visible = false;
				_visible = false;
			}
			else if(b && !_visible){
				for each(p in mesh.aPolygon) p.visible = true;
				_visible = true;
			}
		}
		
		public function get bsqr():BSqr{ return _bsqr; }
		public function get base():Base3d{ return _base; }
		public function get visible():Boolean{ return _visible; }
		
	}
}